package dao

import (
	"encoding/json"
)

type UserCompany struct {
	BaseModel
	UserId    int
	CompanyId int
	MainFlg   string
	UserFlg   string
}

func (c *UserCompany) TableName() string {
	return "user_company"
}

func (c *UserCompany) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func SelectAllUserCompany() *[]UserCompany {
	userCompanies := make([]UserCompany, 0)
	DB.Where("is_del = ?", 0).Find(&userCompanies)
	return &userCompanies
}

func SelectUserCompanyByExample(userCompany *UserCompany) *[]UserCompany {
	userCompanies := make([]UserCompany, 0)
	DB.Where(&userCompany)
	DB.Find(&userCompanies)
	return &userCompanies
}

func SaveUserCompany(userCompany *UserCompany) int {
	DB.Create(&userCompany)
	return userCompany.ID
}

func SaveUserCompanyBatch(userCompanys *[]UserCompany) {
	for _, userCompany := range *userCompanys {
		DB.Create(&userCompany)
	}
}

func UpdateUserCompany(userCompany *UserCompany) {
	DB.Model(&userCompany).Save(&userCompany)
}

func DeleteUserCompany(id int) {
	company := UserCompany{}
	company.ID = id
	DB.Delete(company)
}

func DeleteUserCompanyByExample(userCompany *UserCompany) {
	DB.Where(&userCompany).Delete(UserCompany{})
}
